package com.example.answer_staff;

import java.io.File;

import android.app.Application;
import android.content.Context;

import com.gaosai.staff.domain.Constants;
import com.gaosai.staff.domain.User;
import com.gaosai.staff.utils.SPUtil;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
/**
 * @author willis
 *
 */
public class StaffApplication extends Application {
	
	public static StaffApplication mInstance;
	private User user = null;
	
	private File file ;

	@Override
	public void onCreate() {
		super.onCreate();
		mInstance = this;
		file = new File(Constants.HOME_PATH, "cacheimage") ;
		if (!file.exists()) {
			file.mkdirs();
		}
		initImageLoader(getApplicationContext());
	}
	
	public static StaffApplication getInstance() {
		return mInstance;
	}
	
	public  void initImageLoader(Context context) {
		// This configuration tuning is custom. You can tune every option, you may tune some of them,
		// or you can create default configuration by
		//  ImageLoaderConfiguration.createDefault(this);
		// method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.discCache(new UnlimitedDiscCache(file))
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
	}

	/**
	 * 清除登录的用户信息
	 */
	public void clearUser() {
		SPUtil.clear(getApplicationContext(), "login_user");
		user = null;
		// ("clearUser");
	}

	/**
	 * 获取登录的用户信息
	 */
	public User getUser() {
		if (user == null) {
			user = (User) SPUtil.readObjectToShared(SPUtil.getValue(
					getApplicationContext(), "login_user"));

			// ("getUser"
			// + mSharedPreferences.getString("login_user", null));
		}
		return user;
	}

	/**
	 * 记住登录的用户信息
	 */
	public void setUser(User user) {
		this.user = user;
		if (user != null) {
			SPUtil.putValue(getApplicationContext(), "login_user",
					SPUtil.saveObjectToShared(user));
		}
	}
}
